package com.wang.atm;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args) {
        ArrayList<Account> accounts = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("-----欢迎使用ATM系统-----");
            System.out.println("1、账户登录");
            System.out.println("2、账户开户");
            System.out.println("请您选择操作：");
            int command = sc.nextInt();
            switch (command){
                case 1:
                    login(accounts,sc);
                    break;
                case 2:
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您输入操作命令有误！");
            }
        }
    }

    private static void login(ArrayList<Account> accounts, Scanner sc) {
        while (true) {
            System.out.println("------系统登录操作------");
            if (accounts.size() == 0){
                System.out.println("对不起，当前系统无任何账户，请先开户，再登录！");
                return;
            }
            System.out.println("请您输入卡号：");
            String cardId = sc.next();
            Account acc = getAccountByCardId(cardId,accounts);
            if (acc != null){
                while (true) {
                    System.out.println("请您输入密码：");
                    String passWord = sc.next();
                    if (acc.getPassWord().equals(passWord)){
                        System.out.println("恭喜您，" + acc.getUserName() + "进入系统，" + "您的账户为：" + acc.getCardId());
                        showUserCommand(sc,acc,accounts);
                        return;
                    } else {
                        System.out.println("您输入的密码有误！");
                    }
                }
            } else {
                System.out.println("对不起，系统中不存在该账户！");
            }
        }
    }

    private static void showUserCommand(Scanner sc , Account acc ,ArrayList<Account> accounts) {
        while (true) {
            System.out.println("------用户操作页------");
            System.out.println("1、查询账户");
            System.out.println("2、存款");
            System.out.println("3、取款");
            System.out.println("4、转账");
            System.out.println("5、修改密码");
            System.out.println("6、退出");
            System.out.println("7、注销账户");
            System.out.println("请选择：");
            int command = sc.nextInt();
            switch (command){
                case 1:
                    //查询账户(展示当前登录的账户信息)
                    showAccount(acc);
                    break;
                case 2:
                    //存款
                    depositMoney(acc,sc);
                    break;
                case 3:
                    //取款
                    drawMoney(acc, sc);
                    break;
                case 4:
                    //转账
                    transferMoney(acc,sc,accounts);
                    break;
                case 5:
                    //修改密码
                    updatePassWord(acc,sc);
                    return;
                case 6:
                    //退出
                    System.out.println("退出成功，欢迎下次光临！");
                    return;
                case 7:
                    //注销账户
                    if (deleteAccount(acc,sc,accounts)){
                        //回到首页
                        return;
                    } else {
                        //回到操作页
                        break;
                    }
                default:
                    System.out.println("您输入的操作命令不正确！");
            }
        }
    }

    private static boolean deleteAccount(Account acc, Scanner sc, ArrayList<Account> accounts) {
        System.out.println("------用户销户操作------");
        System.out.println("您真的要销户？y/n");
        String rs = sc.next();
        switch (rs){
            case "y":
                if (acc.getMoney() > 0){
                    System.out.println("您的账户中还有余额，不能进行销户！");
                } else {
                    accounts.remove(acc);
                    System.out.println("您的账户销户完成！");
                    return true;
                }
                break;
            default:
                System.out.println("好的，当前账户继续保留！");
        }
        return false;
    }

    private static void updatePassWord(Account acc, Scanner sc) {
        System.out.println("------用户修改密码------");
        while (true) {
            System.out.println("请输入当前密码：");
            String passWord = sc.next();
            if (acc.getPassWord().equals(passWord)){
                while (true) {
                    System.out.println("请您输入新密码：");
                    String newPassWord = sc.next();
                    System.out.println("请您确认新密码：");
                    String okPassWord = sc.next();
                    if (newPassWord.equals(okPassWord)){
                        acc.setPassWord(newPassWord);
                        System.out.println("您的密码修改成功！");
                        return;
                    } else {
                        System.out.println("您两次输入的密码不一致！");
                    }
                }
            } else {
                System.out.println("您输入的密码不正确！");
            }
        }
    }

    private static void transferMoney(Account acc, Scanner sc, ArrayList<Account> accounts) {
        System.out.println("------用户转账操作------");
        if (accounts.size() < 2){
            System.out.println("当前系统中不足两个账户，不能进行转账！");
            return;
        }
        if (acc.getMoney() == 0){
            System.out.println("您的账户余额不足，无法进行转账！");
        }
        while (true) {
            System.out.println("请您输入对方的卡号：");
            String cardId = sc.next();
            if (cardId.equals(acc.getCardId())){
                System.out.println("对不起，您不能给自己转账！");
                continue;
            }
            Account account = getAccountByCardId(cardId,accounts);
            if (account == null){
                System.out.println("对不起，您输入的账户不存在！");
            } else {
                String username = account.getUserName();
                String tip = "*" + username.substring(1);
                System.out.println("请您输入[" + tip + "]的姓氏：");
                String preName = sc.next();
                if (username.startsWith(preName)){
                    System.out.println("请输入您的转账金额：");
                    double money = sc.nextDouble();
                    if (money < acc.getMoney()){
                        acc.setMoney(acc.getMoney() - money);
                        account.setMoney(acc.getMoney() + money);
                        System.out.println("转账已完成，您的账户余额为：" + acc.getMoney());
                        return;
                    } else {
                        System.out.println("对不起，您的余额不足，最多可以转账" + acc.getMoney());
                    }
                } else {
                    System.out.println("对不起，您输入的信息有误！");
                }
            }
        }
    }

    private static void drawMoney(Account acc, Scanner sc) {
        System.out.println("------用户取钱操作------");
        if (acc.getMoney() < 100){
            System.out.println("对不起，您当前账户余额已不足100元，无法取钱！");
            return;
        }
        while (true) {
            System.out.println("请您输入取款金额：");
            double money = sc.nextDouble();
            if (money > acc.getQuotaMoney()){
                System.out.println("您当前的取款金额大于每次限额，每次限额为：" + acc.getQuotaMoney());
            } else {
                if (money > acc.getMoney()){
                    System.out.println("余额不足，您当前账户的总金额为：" + acc.getMoney());
                } else {
                    System.out.println("恭喜您，取钱" + money + "元成功！");
                    acc.setMoney(acc.getMoney() - money);
                    showAccount(acc);
                }
            }
        }
    }

    private static void depositMoney(Account acc, Scanner sc) {
        System.out.println("------用户存钱操作------");
        System.out.println("请您输入存款金额：");
        double money = sc.nextDouble();
        acc.setMoney(acc.getMoney() + money);
        System.out.println("您已完成存钱操作，当前账户信息如下：");
        showAccount(acc);
    }

    private static void showAccount(Account acc) {
        System.out.println("------当前账户信息如下------");
        System.out.println("卡号：" + acc.getCardId());
        System.out.println("户主：" + acc.getUserName());
        System.out.println("余额：" + acc.getMoney());
        System.out.println("限额：" + acc.getQuotaMoney());
    }

    private static void register(ArrayList<Account> accounts, Scanner sc) {
        Account account = new Account();
        System.out.println("-----系统开户操作-----");
        System.out.println("请输入用户名：");
        String userName = sc.next();
        account.setUserName(userName);
        while (true) {
            System.out.println("请输入密码：");
            String passWord = sc.next();
            System.out.println("请您输入确认密码：");
            String okPassWord = sc.next();
            if (okPassWord.equals(passWord)){
                account.setPassWord(okPassWord);
                break;
            } else {
                System.out.println("您输入的两次密码不一致，请重新确认！");
            }
        }
        System.out.println("请您输入账户当次限额：");
        double quotaMoney = sc.nextDouble();
        account.setQuotaMoney(quotaMoney);

        //为账户随机产生一个8位且与其他账户不重复的号码
        String cardId = getRandomCardId(accounts);
        account.setCardId(cardId);
        accounts.add(account);
        System.out.println("恭喜您开户成功，您的卡号为：" + cardId);
    }

    private static String getRandomCardId(ArrayList<Account> accounts) {
        String cardId = "";
        Random r = new Random();
        for (int i = 0; i < 8; i++) {
            cardId += r.nextInt(10);
        }
        Account acc = getAccountByCardId(cardId, accounts);
        if (acc == null){
            return cardId;
        }
        return null;
    }

    private static Account getAccountByCardId(String cardId, ArrayList<Account> accounts) {
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId)){
                return acc;
            }
        }
        return null;
    }
}
